package com.whucke.im.service.impl;

import com.alibaba.fastjson.JSON;
import com.whucke.im.been.User;
import com.whucke.im.cache.UserCahce;
import com.whucke.im.enums.MessageType;
import com.whucke.im.http.Uri;
import com.whucke.im.service.IUserService;
import com.whucke.im.utils.HttpUtil;
import com.whucke.im.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户业务实现
 *
 * @author Zhang Peng
 * @since 2019/2/23 20:43
 */
@Service
public class UserServiceImpl implements IUserService {
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String NICKNAME = "nickname";
    @Autowired
    private HttpUtil httpUtil;
    @Value("${im.server}")
    private String imServer;

    @Override
    public JsonResult login(String username, String password) {
        return loginRequest(username, password);
    }

    @Override
    public JsonResult register(String username, String password, String nickname) {
        return registerRequest(username, password, nickname);
    }

    @Override
    public JsonResult userList() {
        return userListRequest();
    }

    @Override
    public JsonResult selectUser(String username) {
        JsonResult result = new JsonResult();
        if (StringUtils.isEmpty(username)) {
            result.setMsg("请输入选择用户账号");
            return result;
        }
        if (UserCahce.getLoginUser() == null) {
            result.setMsg("请先登录");
            return result;
        }
        if (username.equals(UserCahce.getLoginUser().getUsername())) {
            result.setMsg("不能选择自己");
            return result;
        }
        //获取用户列表，校验用户是否存在
        Map<String, User> userList = UserCahce.getUserList();
        if (!userList.containsKey(username)) {
            result.setMsg("用户不存在");
            return result;
        }
        result.setBody(username);
        result.setStatus(true);
        return result;
    }

    @Override
    public JsonResult sendMsg(String content, MessageType type) {
        JsonResult result = new JsonResult();
        if (StringUtils.isEmpty(content)) {
            result.setMsg("消息不能为空");
            return result;
        }
        if (type == null) {
            result.setMsg("消息类型不能为空");
            return result;
        }
        Map<String, String> params = new HashMap<>();
        params.put("sendUserId", UserCahce.getLoginUser().getId());
        params.put("receiveId", UserCahce.selectId);
        params.put("type", UserCahce.sendType.getCode());
        params.put("content", content);
        return httpUtil.doPost(imServer + Uri.SEND_MSG_REQ, JSON.toJSONString(params));
    }

    /**
     * 用户列表
     *
     * @return
     */
    private JsonResult userListRequest() {
        String url = imServer + Uri.USER_LIST_REQ;
        return httpUtil.doGet(url, null);
    }

    private JsonResult registerRequest(String username, String password, String nickname) {
        JsonResult result = new JsonResult();
        if (StringUtils.isEmpty(username)) {
            result.setMsg("账号不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            result.setMsg("密码不能为空");
        }
        if (StringUtils.isEmpty(nickname)) {
            result.setMsg("昵称不能为空");
        }
        //发送请求
        String url = imServer + Uri.REGISTER_REQ;
        Map<String, String> params = new HashMap<>();
        params.put(USERNAME, username);
        params.put(PASSWORD, password);
        params.put(NICKNAME, nickname);
        result = httpUtil.doPost(url, JSON.toJSONString(params));
        return result;
    }

    /**
     * 登录请求
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    private JsonResult loginRequest(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return new JsonResult(false, "用户名或密码不能为空");
        }
        String url = imServer + Uri.LOGIN_REQ;
        Map<String, String> params = new HashMap<>();
        params.put(USERNAME, username);
        params.put(PASSWORD, password);
        return httpUtil.doPost(url, JSON.toJSONString(params));
    }
}
